<template>
	<view class="content bgc-f0">
		<view class="p20 bgc-fff">
			<u-swiper :list="list" height="320"></u-swiper>
		</view>

		<view class="p20">
			<view class="subtitle"><text class="c-theme">全部项目</text></view>

			<!--全部项目-->
			<view class="_list z-flex z-flex-between" style="flex-wrap: wrap;">
<!-- 				<view class="_li" @click="handDefinePrice">
					<view class="bd1" style="width: 100%;min-height: 300rpx;max-height: 400rpx;"></view>
					<view class="c-666 fs28 mt12 mb14">补差价产品</view>
					<view class="c-red">
						<text class="fs26">￥</text> 
						<text class="fs24 ml4">自定义价格</text> 
					</view>
				</view> -->
				<template v-for="item in proList">
					<view class="_li" :key="item.id" @click="handViewShop(item.id)">
						<u-image :src="item.cover" width="100%" height="300rpx"></u-image>
						<view class="c-666 fs28 mt12 mb14 u-line-1">{{item.name}}</view>
						<view class="c-red">
							<text class="fs26">￥</text>
							<text class="fs30 ml6">{{item.price}}</text> 
							<!-- <text class="fs24 ml4">元/次</text> -->
						</view>
					</view>
				</template>
			</view>
		</view>
		<!--空数据-->
		<vempty type="more" v-if="datalen" title="没有更多数据了"></vempty>
	</view>
</template>

<script>
	import { productListApi, getOneApi, changeBusinessApi } from '@/api/shop.js'
	import { bannerListApi } from '@/api/banner.js'
	import { getQueryString } from '@/libs/function.js'
	import { businessMemberApi } from '@/api/user.js'
	export default {
		data() {
			return {
				option: null,
				storeInfo: null,
				list: [],
				proList: [],
				current: 0,
				datalen: false,
				url: ''
			}
		},
		onLoad(query) {
			const q = decodeURIComponent(query.q)
			if(q && q !== 'undefined'){
				uni.setStorageSync('userUrl',q)
				const businessId = getQueryString(q,'b')
				if(businessId){
					uni.setStorageSync('businessId',Number(businessId))
				}
			}
		},
		async onShow() {
			await this.$onLaunched
			this.getProList()
		},
		onPullDownRefresh() {
			this.getProList()
		},
		methods: {
			async getProList(){
				const url = uni.getStorageSync('userUrl')
				const customId = getQueryString(url,'a')
				const businessId = getQueryString(url,'b')
				const params = businessId?{businessId: Number(businessId)}:{}
				if(customId && businessId){
					const userId = uni.getStorageSync('userId')
					if(userId){
						this.userAuth(customId,businessId)
						this.changeStore(businessId)
						uni.setStorageSync('businessId',Number(businessId))
					}
				}
				/* 依次获取分享的门店，切换门店， */
				const businessIdStorage = uni.getStorageSync('businessId')
				const query = params.businessId?params:(businessIdStorage?{businessId: businessIdStorage}:params)
				uni.showLoading({
					title: '数据加载中...'
				})
				const {code,data} = await productListApi(query)
				uni.hideLoading()
				uni.stopPullDownRefresh()
				if(code !=0 ) return
				this.proList = data.list
				this.datalen = data.list.length == 0
				if(query.businessId){
					this.getStoreInfo(query)
				}else{
					this.getBannerList()
				}
			},
			// 获取门店信息
			async getStoreInfo(query){
				const {code,data} = await getOneApi({
					id: query.businessId
				})
				if(code != 0) return
				this.list = data.img.map(it=>{
					return {image: it}
				})
			},
			// 获取banner信息
			async getBannerList(){
				const {code,data} = await bannerListApi()
				if(code != 0) return
				this.list = data.list.map(it=>{
					it.image = it.url
					return it
				})
			},
			// 成为店小二
			async userAuth(c,b){
				const {code, data} = await businessMemberApi({
					memberId: Number(uni.getStorageSync('userId')),
					businessId: Number(b),
					inviteId: Number(c)
				})
			},
			// 改变门店
			async changeStore(a){
				const {code,data} = await changeBusinessApi({
					businessId: Number(a),
					userId: Number(uni.getStorageSync('userId'))
				})
			},
			change(index) {
				this.current = index;
			},
			handViewShop(id) {
				uni.$u.route({
					type: 'navigateTo',
					url: 'mapPages/shop_detail/index',
					params: {
						id
					}
				})
			},
			handDefinePrice() {
				uni.$u.route({
					type: 'navigateTo',
					url: 'mapPages/define_price/index'
				})
			}
		}
	}
</script>

<style scoped lang="scss">
	.content {
		width: 100vw;
		height: 100vh;
		overflow: auto;

		._list {
			._li {
				width: 48.5%;
				height: auto;
				background-color: #fff;
				padding: 30rpx;
				border-radius: 12rpx;
				margin-bottom: 24rpx;
			}
		}
	}
</style>
